package com.web.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by reeveslee on 16/6/28.
 */
public class WechatRequest {
    private static String appId = "wx651efc7e955f2b3c";
    private static String secret = "05a7e65039e5e736c2fd5c9c9865eef6";
    private static final Logger LOGGER = Logger.getLogger(WechatRequest.class);

    /**
     * 获取access token
     *
     * @param code
     * @return
     */
    public static Map<String, Object> accessToken(String code) {
        Map<String, Object> result = new HashMap<>();

        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
        String wechat_access_token = HttpRequest.sendGet(url);
        JSONObject jsonObject = JSONObject.parseObject(wechat_access_token);

        if (jsonObject.get("access_token") == null) {
            // 获取access_token失败
            LOGGER.error("failed to get access token,message return is : " + jsonObject.toJSONString());
            result.put("access_token", "");
            result.put("openid", "");
            result.put("refresh_token", "");
        } else {
            result.put("access_token", jsonObject.getString("access_token"));
            result.put("openid", jsonObject.getString("openid"));
            result.put("refresh_token", jsonObject.getString("refresh_token"));
        }

        return result;
    }


    /**
     * 刷新access_token
     *
     * @param refresh_token
     * @return
     */
    public static Map<String, Object> refreshAccessToken(String refresh_token) {
        Map<String, Object> result = new HashMap<>();

        String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + appId + "&grant_type=refresh_token&refresh_token=" + refresh_token;
        String wechat_access_token = HttpRequest.sendGet(url);
        JSONObject jsonObject = JSONObject.parseObject(wechat_access_token);

        if (jsonObject.get("access_token") == null) {
            // 获取access_token失败
            LOGGER.error("failed to refresh access token,message return is : " + jsonObject.toJSONString());
            result.put("access_token", "");
            result.put("openid", "");
        } else {
            result.put("access_token", jsonObject.getString("access_token"));
            result.put("openid", jsonObject.getString("openid"));
        }

        return result;
    }


    /**
     * 获取登陆用户信息
     *
     * @param access_token
     * @param openid
     * @return
     */
    public static Map<String, Object> getUserInfo(String access_token, String openid) {
        Map<String, Object> result = new HashMap<>();

        String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
        String wechat_access_token = HttpRequest.sendGet(url);
        JSONObject jsonObject = JSONObject.parseObject(wechat_access_token);

        if (jsonObject.get("openid") == null) {
            // 获取access_token失败
            LOGGER.error("failed to get user info,message return is : " + jsonObject.toJSONString());
            result.put("openid", "");
            result.put("nickname", "");
            result.put("headimgurl", "");
            result.put("province", "");
            result.put("city", "");

        } else {
            result.put("openid", jsonObject.getString("openid"));
            result.put("nickname", jsonObject.getString("nickname"));
            result.put("headimgurl", jsonObject.getString("headimgurl"));
            result.put("province", jsonObject.getString("province"));
            result.put("city", jsonObject.getString("city"));
        }

        return result;
    }


}
